package com.cty.jsp;


import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.net.http.HttpRequest;

@Controller
@RequestMapping("jsp")
public class JspController {

    @RequestMapping("index")
    //注意不要写@ResponseBody该注解，会跳过视图解析器
    public String index(HttpServletRequest request) {
        request.setAttribute("data","hello jsp!!!!");
        return "index";
    }

    //转发
    /*
    * 返回的字符串要加forward:+路径
    *  注意：还是不加@ResponseBody
    *
    * */
    @RequestMapping("forward")
    public String forward() {
        System.out.println("JspController.forward");
        return "forward:/jsp/index";
    }

    //重定向
    /*
    *
    *   返回路径要加redirect:+路径
    *   注意:因为重定向是二次请求，但是由于springmvc的优化，前面依然可以不加根路径(本项目下的资源)
    */
    @RequestMapping("redirect")
    public String redirect() {
        System.out.println("JspController.redirect");
        return "redirect:/jsp/index";
    }

    @RequestMapping("redirect/baidu")
    public String redirectBaidu() {
        System.out.println("JspController.redirectBaidu");
        return "redirect:http://www.baidu.com";
    }

}
